package com.example.permission.controller;

import com.example.permission.common.page.Page;
import com.example.permission.common.result.Result;
import com.example.permission.entity.dto.RoleMenuDTO;
import com.example.permission.entity.dto.SysRoleDTO;
import com.example.permission.entity.dto.page.SysRolePageDTO;
import com.example.permission.entity.po.SysRole;
import com.example.permission.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 角色信息
 *
 * @author xuwq
 * @date 2020-05-09 15:56:07
 */
@RestController
@Api(tags = "角色信息")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @PostMapping("v1/role")
    @ApiOperation("新增")
    public Result<SysRole> add(@RequestBody @Valid SysRoleDTO sysRoleDTO) {
        return Result.success(this.sysRoleService.insert(sysRoleDTO));
    }

    @PutMapping("v1/role/{roleId}")
    @ApiOperation("修改")
    public Result<SysRole> edit(@PathVariable("roleId") Integer roleId, @RequestBody @Valid SysRoleDTO sysRoleDTO) {
        sysRoleDTO.setRoleId(roleId);
        return Result.success(this.sysRoleService.update(sysRoleDTO));
    }

    @DeleteMapping("v1/role/{roleId}")
    @ApiOperation("删除")
    public Result<Boolean> deleteById(@PathVariable("roleId") Integer roleId) {
        return Result.success(this.sysRoleService.deleteById(roleId));
    }

    @GetMapping("v1/role/{roleId}")
    @ApiOperation(value = "根据id查询数据")
    public Result<SysRole> queryById(@PathVariable("roleId") Integer roleId) {
        return Result.success(this.sysRoleService.queryById(roleId));
    }

    @PostMapping("v1/role/multi")
    @ApiOperation(value = "查询分页列表")
    public Result<Page<SysRole>> queryListByPage(@RequestBody @Valid SysRolePageDTO sysRolePageDTO) {
        return Result.success(this.sysRoleService.queryListByPage(sysRolePageDTO));
    }

    @PutMapping("v1/role/{roleId}/menu")
    @ApiOperation(value = "角色菜单分配")
    public Result roleMenuDistribution(@PathVariable("roleId") Integer roleId, @RequestBody @Valid RoleMenuDTO roleMenuDTO) {
        this.sysRoleService.roleMenuDistribution(roleId, roleMenuDTO);
        return Result.success();
    }

}